package com.hz.config;

import com.hz.dao.EarningsDao;
import com.hz.pojo.Earnings;
import com.hz.utils.DateUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;

/**
 * 每天定时计算平台收益存入数据库
 */
@Configuration      //1.主要用于标记配置类
@EnableScheduling   // 2.开启定时任务
public class EarningsConfig {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    EarningsDao earningsDao;
    @Scheduled(cron = "0 0 1 * * ?")
    public void updEarnings(){
        Double earningsMoney = 0.00;
        List<Earnings> listEarnings = null;
        try {
            listEarnings = earningsDao.getAllEarnings(null,null);
            //获取之前所有记录
            for (Earnings earnings:listEarnings) {
                earningsMoney+=earnings.getEarnings_money();
            }
            //计算截至今天的收益
            Double dayMoney=Double.parseDouble(stringRedisTemplate.opsForValue().get("HN520"));
            dayMoney-=earningsMoney;
            earningsDao.addEarnings(DateUtils.getCurrentTime(),dayMoney);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
